local skill = fk.CreateSkill{
  name = "tea__zuojing",
  tags = { Skill.Limited }
}
local U = require "packages/utility/utility"
---@type ViewAsSkillSpec
local zuojing = {
  pattern = "nullification;.|.|.|.|.|basic",
  mute = true,
  interaction = function(self, player)
    local all_names = Fk:getAllCardNames("b")
    table.insert(all_names, "nullification")
    local names = player:getViewAsCardNames("tea__zuojing", all_names)
    if #names > 0 then
      return UI.CardNameBox { choices = names, all_choices = all_names }
    end
  end,
  card_filter = function(self, player, to_select, selected)
    return false
  end,
  view_as = function(self, cards)
    if not self.interaction.data then return end
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = skill.name
    return card
  end,
  before_use = function(self, player, use)
    local room = player.room
    room:notifySkillInvoked(player, skill.name, "big")
    if use.card.type == Card.TypeBasic then
      player:broadcastSkillInvoke(skill.name, 1)
    else
      player:broadcastSkillInvoke(skill.name, 2)
    end
  end,
  after_use = function(self, player, use)
    local name = Fk:cloneCard(self.interaction.data).trueName
    player.room:setPlayerMark(player, "@zuojing_record", name)
  end,
  enabled_at_play = function(self, player)
    return player:usedSkillTimes(skill.name, Player.HistoryGame) == 0
  end,
  enabled_at_response = function(self, player, response)
    return not response and player:usedEffectTimes(skill.name, Player.HistoryGame) == 0
  end,
}
---@type TrigSkelSpec<UseCardFunc>
local zuojing_refresh = {
  can_refresh = function(self, event, target, player, data)
    if player == target and player:getMark("@zuojing_record") == data.card.trueName then
      local room = player.room
      local turn_event = room.logic:getCurrentEvent():findParent(GameEvent.Turn, true)
      local cards = {}
      if turn_event then
        room.logic:getEventsByRule(GameEvent.UseCard, 1, function(e)
          local use = e.data
          if use.card.trueName == player:getMark("@zuojing_record") and use.from == player then
            table.insert(cards, use.card)
          end
          return false
        end, turn_event.id)
      end
      return #cards == 1
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local choice = room:askToChoice(player, {
      choices = {"tea__zuojing_refresh", "tea__zuojing_drawcards", "Cancel"},
      skill_name = skill.name,
      prompt = "#tea__zuojing_choice",
    })
    if choice == "tea__zuojing_refresh" then
      room:notifySkillInvoked(player, skill.name, "special")
		  player:broadcastSkillInvoke(skill.name, 4)
      player:setSkillUseHistory("tea__zuojing", 0, Player.HistoryGame)
      player.room:setPlayerMark(player, "@zuojing_record", 0)
    elseif choice == "tea__zuojing_drawcards" then
      room:notifySkillInvoked(player, skill.name, "drawcard")
		  player:broadcastSkillInvoke(skill.name, 3)
      local to = room:askToChoosePlayers(player, {
        targets = room.alive_players,
        skill_name = skill.name,
        min_num = 1,
        max_num = 1,
        prompt = "#tea__zuojing_choose",
      })
      if #to > 0 then
        to[1]:drawCards(2, skill.name)
      end
    end
  end,
}
skill:addEffect("viewas", zuojing)
skill:addEffect(fk.CardUsing, zuojing_refresh)
return skill